<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<!-- head 头部-->
<head th:replace="admin/layout/include::head('角色管理','')"></head>

<body>
<!-- loading 动画-->
<div th:replace="admin/layout/include::loading"></div>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">
                <div class="pull-left layui-form-pane search-box">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>


            <!-- 数据表格 -->
            <table class="layui-table" id="role-table" lay-filter="role-table"></table>
        </div>
    </div>
</div>
</body>

<!-- 表格操作列 -->
<script type="text/html" id="role-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" ajax-event="del" ajax-url="/admin/role/" value="{{d.id}}">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="auth">权限管理</a>
</script>
<!-- js部分 -->
<div th:replace="admin/layout/include::js"></div>
<script>
    layui.use(['form', 'table', 'util', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        var formUrl;  // 表单提交的action

        //渲染表格
        table.render({
            elem: '#role-table',
            url: '/admin/role/',
            page : true,
			loading : true,
            height: 'full-110',
			limit : 10,
            toolbar:true,
			limits : [ 10, 20, 50, 100 ],
            response:{
	        	countName: 'total'
	        },
            cols: [
                [
                {type: 'checkbox'},
                {field: 'roleName', sort: true, title: '角色名'},
                {field: 'roleCode', sort: true, title: '角色编码'},
                {field: 'roleType', sort: true,align:'center',title: '角色类型',templet:function(d){
                	if (d.roleType == 1) {
                        return '<span class="layui-badge layui-bg-blue">管理角色</span>';
                    }else if(d.roleType == 2){
                    	return '<span class="layui-badge layui-bg-red">VIP角色</span>';
                    }else{
                    	return '';
                    }
                }},
                {field: 'description', sort: true, title: '备注'},
                {field: 'orderNo', sort: true, title: '排序'},
                {
                    field: 'createTime', sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#role-table-bar', title: '操作'}
            ]
            ]
        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });

        // 表单提交事件
        form.on('submit(role-form-submit)', function (data) {
            B.post({
            	url:'/admin/role/',
            	loading:true,
            	data:data.field,
            	success:function(data){
            		if(data.success){
            			 layer.closeAll('page');
                         layer.msg(data.msg, {icon: 1});
                         table.reload('role-table');
            		}else{
            			layer.msg(data.msg, {icon: 2});
            		}
            	}
            });
            return false;
        });

        // 工具条点击事件
        table.on('tool(role-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            } else if (obj.event === 'auth') {  // 权限管理
                showPermDialog(obj.data.id);
            }
        });

        // 搜索按钮点击事件
        $('#role-btn-search').click(function () {
            var keyword = $('#role-edit-search').val();
            table.reload('role-table', {where: {roleName: keyword}});
        });

        // 显示编辑弹窗
        var showEditModel = function (data) {
            admin.putTempData("t_role",data);
            admin.putTempData("formOk",false);
            top.layui.admin.open({
                type: 2,
                title: data ? '修改角色' : '添加角色',
                area: ['380px', '460px'],
                content: '/admin/role/save',
                end: function () {
                    admin.getTempData('formOk') && table.reload('role-table');  // 成功刷新表格
                }
            });

        };

        // 权限管理
        function showPermDialog(roleId) {
            top.layui.admin.open({
                type: 2,
                title: '角色权限分配',
                area: ['380px', '455px'],
                content: '/admin/role/auth?roleId=' + roleId
            });
        }

    });

</script>
</html>